package com.atcumt.Thread.create;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class ThreadTest {

    public static void main(String[] args) throws Exception {
        // 方式一
        MyThread t1 = new MyThread();
        t1.start();

        // 方式二
        Thread t2 = new Thread(new MyRunnable());
        t2.start();

        // 方式三
        FutureTask<String> ft = new FutureTask<>(new MyCallable());
        Thread t3 = new Thread(ft);
        t3.start();
        System.out.println(ft.get());

        // 方式四
        // ThreadPoolExecutor executorOne = new ThreadPoolExecutor(5, 5, 1,TimeUnit.MINUTES, 
        // new ArrayBlockingQueue<Runnable>(20)); // new CustomizableThreadFactory("Thread-pool")
        // executorOne.execute(() -> {
        //     System.out.println("通过线程池创建线程");
        // });
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new MyRunnable());
        }
        executorService.shutdown();
        }
}

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("继承Thread实现多线程:" + Thread.currentThread().getName());
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Runnable实现多线程:" + Thread.currentThread().getName());
    }
}

class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("Callable实现多线程:" + Thread.currentThread().getName());
        return "这是返回值";
    }
}
